﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Planning.Forms
{
    public partial class paramForm : Form
    {
        public paramForm()
        {
            InitializeComponent();
            adrBaseText.Text = Properties.Settings.Default.adrBase;
            nomBaseText.Text = Properties.Settings.Default.nomBase;
            userBaseText.Text = Properties.Settings.Default.userBase;
            pwdBaseText.Text = Properties.Settings.Default.pwdBase;
            erreurSqlCb.Checked = Properties.Settings.Default.erreurSql;

            hauteurText.Text = Properties.Settings.Default.hauteurBouton.ToString();
            largeurText.Text = Properties.Settings.Default.largeurBouton.ToString();
            decalageText.Text = Properties.Settings.Default.decalageBouton.ToString();
            decalageInitText.Text = Properties.Settings.Default.decalageInitBouton.ToString();

            ouvertureText.Text = Properties.Settings.Default.ouverture.ToString();
            fermetureText.Text = Properties.Settings.Default.fermeture.ToString();
        }

        private void validButton_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.adrBase = adrBaseText.Text;
            Properties.Settings.Default.nomBase = nomBaseText.Text;
            Properties.Settings.Default.userBase = userBaseText.Text;
            Properties.Settings.Default.pwdBase = pwdBaseText.Text;
            Properties.Settings.Default.erreurSql = erreurSqlCb.Checked;

            Properties.Settings.Default.hauteurBouton = int.Parse(hauteurText.Text);
            Properties.Settings.Default.largeurBouton = int.Parse(largeurText.Text);
            Properties.Settings.Default.decalageBouton = int.Parse(decalageText.Text) ;
            Properties.Settings.Default.decalageInitBouton = int.Parse(decalageInitText.Text);

            Properties.Settings.Default.ouverture = int.Parse(ouvertureText.Text);
            Properties.Settings.Default.fermeture = int.Parse(fermetureText.Text);

            Properties.Settings.Default.Save();

            this.Close();
        }

        private void testConnexion_Click(object sender, EventArgs e)
        {
            using (SqlConnection connexion = new SqlConnection("user id=" + userBaseText.Text + "; " +
                    "password=" + pwdBaseText.Text + "; " +
                    "server=" + adrBaseText.Text + "; " +
                    "database=" + nomBaseText.Text + "; " +
                    "connection timeout=30")) {
                        try
                        {
                            connexion.Open();
                            connexion.Close();
                            MessageBox.Show("Connexion effectuée avec succès");
                        }
                        catch (SqlException ex) {
                            if (erreurSqlCb.Checked)
                            {
                                MessageBox.Show("Echec de connexion à la base de données..." + ex, "Erreur !", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                                return;
                            }
                            else
                            {
                                MessageBox.Show("Echec de connexion à la base de données...\n\nVérifier les paramètres", "Erreur !", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                                return;
                            }
                        }
                }
        }

    }
}
